'use strict';
/**
 * Created by FreezeSoul on 2015/10/18.
 */
define([
        'angular',
        'app/modules/dataset/define',
        'app/modules/dataset/services/dataset',
        'app/modules/dataset/directives/excel-table'
    ],
    function (angular) {
        angular.module('ad.dataset')
            .controller('adDataSetExcelController', ['$scope', '$stateParams', '$state', '$uibModal', '_', 'Notification', 'adDataSetService'
                , function ($scope, $stateParams, $state, $uibModal, _, Notification, dataSetService) {
                    $scope.excelData = undefined;
                    $scope.handsontable = undefined;
                    $scope.loadExcel = function () {
                        dataSetService.loadExcelData($scope.dataSet.dataset_code).then(function (data) {
                            var obj = angular.fromJson(data);
                            if (obj.result == 'success') {
                                $scope.excelData = obj.data;
                            }
                        });
                    };

                    $scope.update = function () {
                        if ($scope.handsontable) {
                            dataSetService.updateExcelData($scope.dataSet.dataset_code, $scope.handsontable.getData()).then(function (data) {
                                var obj = angular.fromJson(data);
                                if (obj.result == 'success') {
                                    Notification.success('数据更新成功！');
                                    $state.go('editor.schema.schema-edit', {schemaid: $scope.$parent.$stateParams.schemaid});
                                } else {
                                    Notification.error('数据更新失败！');
                                    $scope.loadExcel();
                                }
                            });
                        }
                    };

                    $scope.loadExcel();
                }]);
    });